package com.qyl.honghengshop.controller.customer;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dao.impl.GoodsTypeDaoImpl;
import com.qyl.honghengshop.dto.GoodTypeManager;
import com.qyl.honghengshop.dto.Goods;
import com.qyl.honghengshop.dto.IndexGoods;
import com.qyl.honghengshop.dto.PictureCarousel;
import com.qyl.honghengshop.dto.User;
import com.qyl.honghengshop.service.AdminUserService;
import com.qyl.honghengshop.service.GoodsService;
import com.qyl.honghengshop.service.GoodsTypeService;
import com.qyl.honghengshop.service.PictureService;
import com.qyl.honghengshop.service.UserService;
import com.qyl.honghengshop.service.impl.AdminUserServiceImpl;
import com.qyl.honghengshop.service.impl.GoodsServiceImpl;
import com.qyl.honghengshop.service.impl.GoodsTypeServiceImpl;
import com.qyl.honghengshop.service.impl.PictureServiceImpl;
import com.qyl.honghengshop.service.impl.UserServiceImpl;
import com.qyl.honghengshop.utils.CodeGenerate;
import com.qyl.honghengshop.utils.FileUpload;
import com.qyl.honghengshop.utils.MD5;
import com.qyl.honghengshop.utils.MailUtils;

/**
 * 前台登录controller
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/customer")
public class UserController {
	
	private UserService userService = new UserServiceImpl();
	private PictureService pictureService = new PictureServiceImpl();
	private GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
	private GoodsService goodsService = new GoodsServiceImpl();
	private AdminUserService adminUserService = new AdminUserServiceImpl();
	
	//跳转到home页面
	@RequestMapping("/home")
	public String toHome(ModelMap map){
	
		//获得所有轮播图
		List<PictureCarousel> pictureList = pictureService.allPictureList();

		//获得热门商品
		List<Goods> goodsList = goodsService.getGoodsListIsHot();
		
		//获得首页所有栏位数据
		List<IndexGoods> indexGoodsList = goodsService.getIndexGoodsList();
	
		map.put("pictureList", pictureList);
	
		map.put("goodsList", goodsList);
		
		map.put("indexGoodsList", indexGoodsList);
		
		return "home";
	}
	//跳转到登录界面
	@RequestMapping("/toLogin")
	public String toLogin(){
		
		return "login";
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public Map<String,String> login(String userId,String pwd,String code,
			String rember,HttpSession session,HttpServletResponse response) throws Exception{
		
		Map<String, String> map = new HashMap<String, String>();
		
		String sessionCode = (String) session.getAttribute(PageConstant.AUTH_CODE);
		
		//判断验证码是否正确
		if(sessionCode.equals(code)){
			
			//把密码转成md5
			String newPwd = MD5.getMD5(pwd);
			
			//调用service获得用户
			User user = userService.getUserByUserIdAndPwd(userId, newPwd);
			//用户存在
			if(null != user){
				
				if(user.getActive_status() == 1){
					//把用户放到session中
					session.setAttribute(PageConstant.SESSION_USER, user);
					//设置状态为成功
					map.put("status", "success");
					
					//是否记住密码
					if("1".equals(rember)){					
						Cookie  cookie = new Cookie("remberUser",user.getUser_id()+"-"+user.getPass_word()); 			
						cookie.setMaxAge(60*60*24);					
						cookie.setPath("/");
						response.addCookie(cookie);					
					}				
				}else{
					//用户不存在
					map.put("status", "fail");
					map.put(PageConstant.TIP, "用户名未激活");	
				}
								
			}else{
				//用户不存在
				map.put("status", "fail");
				map.put(PageConstant.TIP, "用户名或者密码错误");	
			}
			
		}else{			
			map.put("status", "fail");
			map.put(PageConstant.TIP, "验证码错误");			
		}
		
		return map;
	}
	
	@RequestMapping("/getGoodsTypeAjax")
	@ResponseBody
	public GoodTypeManager getGoodsTypeAjax(){
		
		GoodTypeManager goodTypeManger = goodsTypeService.getGoodTypeManger();
		
		return goodTypeManger;
		
	}
	
	@RequestMapping("/addUser")
	public String addUser(User user,MultipartFile headImg,ModelMap map) throws Exception{
		
		String fileName = FileUpload.fileUpload(headImg);
		
		user.setUserHeadImg(fileName);
		
		String newPwd = MD5.getMD5(user.getPass_word());
		
		user.setPass_word(newPwd);
		
		int count = adminUserService.addUser(user);
		
		if(count>0){
			map.put(PageConstant.TIP, "添加用户成功");
		}else{
			map.put(PageConstant.TIP, "添加用户失败");			
		}		
		return "user";		
	}
	
	@RequestMapping("/toRegister")
	public String toRegister(){		
		
		return "register";
	}
	
	@RequestMapping("/register")
	public String register(User user,ModelMap map) throws Exception{
		
		user.setActive_code(CodeGenerate.generateActiveCode());
		String newPwd = MD5.getMD5(user.getPass_word());
		user.setPass_word(newPwd);
		user.setCreate_date(new Date());
		user.setActive_status(0);
		//保存注册用户
		adminUserService.register(user);		
		//发送邮件激活账号
		MailUtils.sendMail(user.getEmail(),"欢迎"+user.getUser_id()+"注册ebuy网站账号", "你的账户注册成功，请点击<a href='http://localhost:8080/honghengshop/customer/activeUser?activeCode="+user.getActive_code()+"'>这里</a>激活你的账号");
		
		map.put(PageConstant.TIP, "注册成功");
		return "login";
	}
	
	@RequestMapping("/activeUser")
	public String activeUser(String activeCode){
		
		adminUserService.activeUser(activeCode);
		
		return "login";
	}
}
